package com.iotechn.unimall.launcher.controller;

import com.alibaba.fastjson.JSON;
import com.iotechn.unimall.core.util.DateUtils;
import com.lin.generator.service.GeneratorService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lin
 * Date: 2018-09-13
 * Time: 上午9:31
 */
@Controller
@RequestMapping("/gen")
public class GeneratorController implements InitializingBean {

    @Autowired
    private GeneratorService generatorService;

//    private List exceptionDefinitionList;

    @RequestMapping("/")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> params = new HashMap<>();

        params.put("_search", "false");
        params.put("limit",  1000);
        params.put("page",  1);
        params.put("order",  "asc");
        params.put("offset" ,  0);


        List<Map<String, Object>> list =  generatorService.queryList(params);
        modelAndView.setViewName("tablelist");
        modelAndView.addObject("model", list);
        return modelAndView;
    }

    /**
     * 生成代码
     */
    @RequestMapping("/code")
    public void code(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String[] tableNames = new String[]{};
        //获取表名，不进行xss过滤
        Map<String, String[]> parameterMap = request.getParameterMap();
//        tableNames = parameterMap.get("tables").toString().split(",");
//        tableNames = parameterMap.get("tables");
//        request.getParameter("tables");
//        tableNames=JSON.parseArray(parameterMap.get("tables").toString()).toArray(tableNames);
        String tables = request.getParameter("tables");
        tableNames = JSON.parseArray(tables).toArray(tableNames);

        byte[] data = generatorService.generatorCode(tableNames,request.getParameter("parentPermission"));

        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"AutoCode"
                + DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN_YYYY_MM_DD_HH_MM_SS_SSS) + ".zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");

        IOUtils.write(data, response.getOutputStream());
    }
//    @RequestMapping("/group/{gp}")
//    public ModelAndView group(@PathVariable("gp") String group) {
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("document");
//        modelAndView.addObject("model", apiManager.generateGroupModel(group));
//        modelAndView.addObject("groupNames", apiManager.generateDocumentModel().getGroups());
//        return modelAndView;
//    }
//
//
//    @RequestMapping("/api/{gp}/{mt}")
//    public ModelAndView api(@PathVariable("gp") String gp, @PathVariable("mt") String mt) {
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("method");
//        modelAndView.addObject("methods", apiManager.methods(gp));
//        modelAndView.addObject("model",apiManager.generateMethodModel(gp,mt));
//        modelAndView.addObject("gp",gp);
//        modelAndView.addObject("exceptionList", exceptionDefinitionList);
//        return modelAndView;
//    }
//
//
    @Override
    public void afterPropertiesSet() throws Exception {
    }
}
